<?php
session_start();
include("mysqlcon.php");

include("ok.php");
include("error.php");
	//$roomType = $_POST['roomType'];
	//$roomID = $_POST['roomID'];
	$extra_bed = $_POST['extraBed'];
	$customer_name = $_POST['customer_name'];
	$customer_surname = $_POST['customer_surname'];
	$customer_id = $_POST['customer_id'];
	$customer_address = $_POST['customer_address'];
	$customer_email = $_POST['customer_email'];
	$customer_tel = $_POST['customer_tel'];
	$customer_fax = $_POST['customer_fax'];
	$reserv_gdetail = $_POST['reserv_gdetail'];
	if($reserv_gdetail == ""){$reserv_gdetail = " "; }
 	$inputDate = $_POST['inputDate'];
 	$error = false;
	$show_extra_bed = $extra_bed == 0 ? '-':$extra_bed;
	$customer_type = $_POST['customer_type'];
	$customer_group_name = $customer_type == 1 ? $_POST['customer_group_name'] : '';
	$userID = $_SESSION["HBD_USER"];
	///
	if($customer_name ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : ชื่อ<br>";}
   if($customer_surname ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : นามสกุล<br>";}
   //if($customer_id ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : รหัสบัตรประจำตัวประชาชน<br>";}
   //if($customer_email ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : อีเมล์<br>";}
   //if($customer_tel ==""){$error=true; $error_text.= "คุณไม่ได้ใส่  : เบอร์โทรศัพท์<br>";}
   //if($inputDate ==""){$error=true; $error_text.= "คุณไม่ได้เลือก : ระยะเวลาในการพัก<br>";}

	if($error==true){error_page($error_text);}
	
	if($error==false){
      list($bYear,$bMonth,$bDay) = explode("-",substr($inputDate,0,10));
      list($eYear,$eMonth,$eDay) = explode("-",substr($inputDate,11,10));

      //$bDate = mktime(0,0,0,$bMonth,$bDay,$bYear) . "<br>";
      $bDate = mktime(0,0,0,$bMonth,$bDay,$bYear) . "<br>";
      $eDate = mktime(0,0,0,$eMonth,$eDay,$eYear);

      $reserv_begin = date("Y-m-d", $bDate);
      $reserv_end = date("Y-m-d", $eDate);

	
      mysql_select_db($dbname,$conn);
      mysql_query('SET NAMES utf8');

	  $chkID = "SELECT MAX(id) FROM customer";
	  $nextID = 0;
	  $roomListArray = Array();
	  $chkIDresult = mysql_query($chkID);
	  while($rs = mysql_fetch_array($chkIDresult))
	  {
		 $nextID =  $rs[0]+1;
	  }
	  
		$sql = "SELECT * FROM roomtype";
		$r = mysql_query($sql,$conn);
		while ($rs = mysql_fetch_array($r))
		{		   
		   $sql2 = "SELECT room_id FROM resort,roomtype WHERE resort.roomtype_id = ".$rs['roomtype_id']." AND room_id NOT IN(SELECT DISTINCT reservation.room_id FROM reservation WHERE reserv_status != '2' AND (`reserv_end` > '$reserv_begin' AND `reserv_end` <= '$reserv_end'
		   OR `reserv_begin` >= '$reserv_begin' AND `reserv_begin` <= '$reserv_end')) AND resort.roomtype_id = roomtype.roomtype_id ORDER BY resort.room_id ASC";
		   $r2 = mysql_query($sql2,$conn); 
		   while ($rs2 = mysql_fetch_array($r2))
			{
				 //$myArray[$i++] = $_POST["roomlist_".$rs2[room_id]];
				 if(isset($_POST["roomlist_".$rs2['room_id']])){
				 	$_POST["roomlist_".$rs2['room_id']] == $rs2['room_id'] ? array_push($roomListArray,$_POST["roomlist_".$rs2['room_id']]) : '';
				 }
				//array_push($myArray,$_POST["roomlist_".$rs2[room_id]]);
			}
		}
		$roomID = implode ( ", ", $roomListArray );
		if($roomID ==""){$error=true; $error_text.= "คุณไม่ได้เลือก : ห้องพัก<br>";}
		if($error==true){error_page($error_text); exit();}
      $chkCustomer="SELECT * FROM customer WHERE customer_name = '".$customer_name."' AND customer_surname = '".$customer_surname."'";
	  $chkResult = mysql_query($chkCustomer,$conn);

	if($customer_id==null)
	{
		if(mysql_num_rows($chkResult)==0)
		{
			 $customerSQL="INSERT INTO customer(customer_name,customer_surname,customer_email,customer_tel,customer_fax, customer_group_name)";
				$customerSQL=$customerSQL."VALUES('$customer_name','$customer_surname','$customer_email','$customer_tel','$customer_fax','$customer_group_name')";
						if(!mysql_query($customerSQL,$conn)){
						echo("ฐานข้อมูลผิดพลาด $customerSQL");}
		}
		else
		{
			while($rs = mysql_fetch_array($chkResult))
			{
				$nextID = $rs[0];	
			}
		}
		
		 	$reservgroupSQLnonMem="INSERT INTO reservgroup(customer_id,room_list,reserv_gdetail,reserv_owner_id)";
			$reservgroupSQLnonMem=$reservgroupSQLnonMem."VALUES('$nextID','$roomID','$reserv_gdetail','$userID')";
			if(!mysql_query($reservgroupSQLnonMem,$conn)){ echo("ฐานข้อมูลผิดพลาด $reservgroupSQLnonMem"); mysql_error();exit();}
			
			$chkreserv_gid = "SELECT MAX(reserv_gid) FROM reservgroup";
			$reserv_gid = 0;
			$resultGID = mysql_query($chkreserv_gid);
			  while($rs = mysql_fetch_array($resultGID))
			  {
				 $reserv_gid =  $rs[0];
			  }
			  $index_count = 0;
$text_room_list = '
		<thead>
		<tr>
		<th scope="col" >ห้องพัก</th>
		<th scope="col" >ราคา</th>
		<th scope="col" >จำนวน(คืน)</th>
		<th scope="col" >เตียงเสริม</th>
		<th scope="col" >รายละเอียดเพิ่มเติม</th>
		<th scope="col" >จำนวนเงิน</th>
		<th scope="col" >ส่วนลด(%)</th>
		<th scope="col" >คิดเป็น</th>
		</tr>	
		</thead>';
		
		$overall_price = 0;	
		$all_extrabed  = 0;			  						  
			foreach($roomListArray as $t){ 
				$reserv_etc_1 = $_POST['reserv_etc_'.$t.''];
				$reserv_extra_bed_1 = $_POST['extraBed_'.$t.''];
				$all_extrabed += $reserv_extra_bed_1;
				$reservSQLnonMem="INSERT INTO reservation(reserv_begin,reserv_end,room_id,id,reserv_etc,reserv_gid,extra_bed)";
				//$reservSQLnonMem=$reservSQLnonMem."VALUES('$reserv_begin','$reserv_end','$t','$nextID','$reserv_etc','$reserv_gid','$extra_bed')";
				$reservSQLnonMem=$reservSQLnonMem."VALUES('$reserv_begin','$reserv_end','$t','$nextID','$reserv_etc_1','$reserv_gid','$reserv_extra_bed_1')";
				if(!mysql_query($reservSQLnonMem,$conn)){ echo("ฐานข้อมูลผิดพลาด $reservSQLnonMem"); exit();}

				$raw_roomcost = mysql_query("SELECT room_cost FROM resort WHERE room_id = '$t'");
				$raw_roomcost = mysql_fetch_array($raw_roomcost);
				$room_cost = $raw_roomcost['room_cost'];
					
				$dateDiff = strtotime($reserv_begin) - strtotime($reserv_end);
	$fullDays = abs(floor($dateDiff/(60*60*24)));
	
	$full_checkin_time = date("d/m/Y",$reserv_begin);
	$full_now = date("d/m/Y",$reserv_end);
	//price calc
	
	//room charge
	$overall_roomprice = $room_cost*$fullDays;
	$overall_price += $overall_roomprice;

				if($index_count%2==0){$text_room_list .= '<tr class="odd">';}
		else{$text_room_list .= '<tr>';}
	//$text_room_list .= "<tr>";
	$text_room_list .= "<td>".$t."</td>";
	$text_room_list .= "<td>".$room_cost."</td>";
	$text_room_list .= "<td>".$fullDays."</td>";
	$text_room_list .= "<td>".$reserv_extra_bed_1."</td>";
	$text_room_list .= "<td>".$reserv_etc_1."</td>";
	$text_room_list .= "<td><div id=".$t.">".$overall_roomprice."</div></td>";
	$text_room_list .= "<td><input id='discount_".$t."' type='text' size='3' autocomplete='off' onKeyup=calByRoom('$t');></td>";
	$text_room_list .= "<td><div id=overall_roomprice_".$t." class='overall_roomprice'>".$overall_roomprice."</div> + ".($reserv_extra_bed_1*400)."</td>";
	$text_room_list .= "</tr>";
	//$text_room_list += '</table>';
		}
		$overall_price = $overall_price+($all_extrabed*400);
		$text_room_list .= "<tr style='background-color:#D5B991;'><td colspan='7' style='text-align:right;padding-right:30px;'><h4>ทั้งหมด</h4></td><td style='color:#CE1815;'><h3 id='overall_price'>$overall_price</h3><input id='overall_price' type='hidden' value='".$overall_price."'><input id='overall_price_discounted' type='hidden' value='$overall_price'></td></tr>";
		$ok_text = "<br>คุณ $customer_name $customer_surname <br>วันที่เข้าพัก ".$reserv_begin." ถึง ".$reserv_end."<hr><br>
				   <table id='table'>$text_room_list</table>
				<p><u>รายละเอียดเพิ่มเติม</u><br>
				$reserv_gdetail</p><input type='hidden' id='reserv_gid' name='reserv_gid' value='$reserv_gid' /><input type='hidden' id='all_extrabed' name='all_extrabed' value='$all_extrabed' /><input type='hidden' id='roomList' name='roomList' value='$roomID' />";
  
  			/*
$ok_text = "<br>คุณ $customer_name $customer_surname <br>เข้าพักตั้งแต่ ".$reserv_begin." ถึง ".$reserv_end."<hr><br>
				   <table id='table'>$text_room_list</table>
				<p style='float:left;'><h2 style='float:right;'><label for='discount'>ส่วนลด : </label> <input type='text' id='discount' name='discount' value='' size='8' /> %</h2></p>
				<input type='hidden' id='reserv_gid' name='reserv_gid' value='$reserv_gid' />";
*/
			ok_page("จัดเก็บข้อมูลเรียบร้อย","ระบบทำการจัดเก็บข้อมูลการจองแล้ว คุณสามารถสั่งพิมพ์ได้ทันที ",$ok_text,"admin.php",$ok_text);
			mysql_close($conn);

	}
	else
	{
			if(mysql_num_rows($chkResult)==0)
			{
				 $customerSQL="INSERT INTO customer(customer_name,customer_surname,customer_address,customer_id,customer_email,customer_tel,customer_fax,customer_group_name)";
					$customerSQL=$customerSQL."VALUES('$customer_name','$customer_surname','$customer_address','$customer_id','$customer_email','$customer_tel','$customer_fax','$customer_group_name')";
							if(!mysql_query($customerSQL,$conn)){
							echo("ฐานข้อมูลผิดพลา $customerSQL");}
			}
			else
			{
				while($rs = mysql_fetch_array($chkResult))
				{
					$nextID = $rs[0];	
				}
			}
			
			$reservgroupSQL="INSERT INTO reservgroup(customer_id,room_list,reserv_gdetail,reserv_owner_id)";
			$reservgroupSQL=$reservgroupSQL."VALUES('$nextID','$roomID','$reserv_gdetail','$userID')";
			if(!mysql_query($reservgroupSQL,$conn)){ echo("ฐานข้อมูลผิดพลาด2 $reservgroupSQL"); echo mysql_error(); exit();}
			
			$chkreserv_gid = "SELECT MAX(reserv_gid) FROM reservgroup";
			$reserv_gid = 0;
			$resultGID = mysql_query($chkreserv_gid);
			  while($rs = mysql_fetch_array($resultGID))
			  {
				 $reserv_gid =  $rs[0];
			  }
			  
			$index_count = 0;
			$text_room_list = '
					<thead>
					<tr>
					<th scope="col" >ห้องพัก</th>
					<th scope="col" >ราคา</th>
					<th scope="col" >จำนวน(คืน)</th>
					<th scope="col" >เตียงเสริม</th>
					<th scope="col" >รายละเอียดเพิ่มเติม</th>
					<th scope="col" >จำนวนเงิน</th>
					<th scope="col" >ส่วนลด(%)</th>
					<th scope="col" >คิดเป็น</th>
					</tr>	
					</thead>';
					
			$overall_price = 0;	
			$all_extrabed  = 0;	
			  
			foreach($roomListArray as $t){ 
				$reserv_etc_2 = $_POST['reserv_etc_'.$t.''];
				$reserv_extra_bed_2 = $_POST['extraBed_'.$t.''];
				$all_extrabed += $reserv_extra_bed_2;
				$reservSQL="INSERT INTO reservation(reserv_begin,reserv_end,room_id,customer_id,id,reserv_etc,reserv_gid,extra_bed)";
				$reservSQL=$reservSQL."VALUES('$reserv_begin','$reserv_end','$t','$customer_id','$nextID','$reserv_etc_2','$reserv_gid','$reserv_extra_bed_2')";
				if(!mysql_query($reservSQL,$conn)){ echo("ฐานข้อมูลผิดพลาด $reservSQL"); exit();}

				$raw_roomcost = mysql_query("SELECT room_cost FROM resort WHERE room_id = '$t'");
				$raw_roomcost = mysql_fetch_array($raw_roomcost);
				$room_cost = $raw_roomcost['room_cost'];
				
				$dateDiff = strtotime($reserv_begin) - strtotime($reserv_end);
				$fullDays = abs(floor($dateDiff/(60*60*24)));
	
				$full_checkin_time = date("d/m/Y",$reserv_begin);
				$full_now = date("d/m/Y",$reserv_end);
				//price calc
				
				//room charge
				$overall_roomprice = $room_cost*$fullDays;
				$overall_price += $overall_roomprice;
			
				if($index_count%2==0){$text_room_list .= '<tr class="odd">';}
				else{$text_room_list .= '<tr>';}
				//$text_room_list .= "<tr>";
				$text_room_list .= "<td>".$t."</td>";
				$text_room_list .= "<td>".$room_cost."</td>";
				$text_room_list .= "<td>".$fullDays."</td>";
				$text_room_list .= "<td>".$reserv_extra_bed_2."</td>";
				$text_room_list .= "<td>".$reserv_etc_2."</td>";
				$text_room_list .= "<td><div id=".$t.">".$overall_roomprice."</div></td>";
				$text_room_list .= "<td><input id='discount_".$t."' type='text' size='3' autocomplete='off' onKeyup=calByRoom('$t');></td>";
				$text_room_list .= "<td><div id=overall_roomprice_".$t." class='overall_roomprice'>".$overall_roomprice."</div> + ".($reserv_extra_bed_2*400)."</td>";
				$text_room_list .= "</tr>";
			}
			
			$overall_price = $overall_price+($all_extrabed*400);
			$text_room_list .= "<tr style='background-color:#D5B991;'><td colspan='7' style='text-align:right;padding-right:30px;'><h4>ทั้งหมด</h4></td><td style='color:#CE1815;'><h3 id='overall_price'>$overall_price</h3><input id='overall_price' type='hidden' value='".$overall_price."'><input id='overall_price_discounted' type='hidden' value='$overall_price'></td></tr>";
			$ok_text = "<br>คุณ $customer_name $customer_surname <br>เข้าพักตั้งแต่ ".$reserv_begin." ถึง ".$reserv_end."<hr><br>
				   <table id='table'>$text_room_list</table>
				<p><u>รายละเอียดเพิ่มเติม</u><br>
				$reserv_gdetail</p>
				<input type='hidden' id='reserv_gid' name='reserv_gid' value='$reserv_gid' /><input type='hidden' id='all_extrabed' name='all_extrabed' value='$all_extrabed' /><input type='hidden' id='roomList' name='roomList' value='$roomID' />";
  
	  			
			/*$ok_text = "เลขที่ใบจอง : $reserv_gid<br>
			ห้องหมายเลข : $roomID<br>
			ชื่อ-นามสกุล ผู้จอง : $customer_name $customer_surname<br>
			ที่อยู่ : $customer_address<br>
			E-Mail : $customer_email<br>
			เบอร์โทรศัพท์ : $customer_tel<br>
			Fax : $customer_fax<br>
			ระยะเวลาในการพัก : $inputDate<br>
			รายละเอียดอื่นๆ : $reserv_gdetail<br>";*/
			ok_page("จัดเก็บข้อมูลเรียบร้อย","ระบบทำการจัดเก็บข้อมูลการจองแล้ว คุณสามารถสั่งพิมพ์ได้ทันที",$ok_text,"admin.php",$ok_text);
			mysql_close($conn);
	}
}
?>